package edu.scau.mis.pos.service.impl;

import edu.scau.mis.pos.entity.PosOrder;
import edu.scau.mis.pos.entity.PosOrderItem;
import edu.scau.mis.pos.mapper.PosItemMapper;
import edu.scau.mis.pos.mapper.PosOrderItemMapper;
import edu.scau.mis.pos.mapper.PosOrderMapper;
import edu.scau.mis.pos.service.IPosOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2021-11-06 21:15:12
 */
@Service("orderService")
public class PosOrderServiceImpl implements IPosOrderService {
    Map<String,List<PosOrderItem>> mapCache = new HashMap<>(); //缓存数据

    @Resource
    private PosOrderMapper posOrderMapper;

    @Resource
    private PosItemMapper posItemMapper;

    @Resource
    private PosOrderItemMapper posOrderItemMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param orderId 主键
     * @return 实例对象
     */
    @Override
    public PosOrder queryOrderWithItemListByOrderId(Integer orderId) {
        return this.posOrderMapper.queryOrderWithItemListByOrderId(orderId);
    }

    /**
     * 分页查询
     *
     * @param posOrder 筛选条件
     * @return 查询结果
     */
    @Override
    public List<PosOrder> queryAll(PosOrder posOrder) {
        return posOrderMapper.queryAll(posOrder);
    }

    /**
     * 新增数据
     *
     * @param posOrder 实例对象
     * @return 实例对象
     */
    @Override
    public PosOrder insert(PosOrder posOrder) {
        this.posOrderMapper.insert(posOrder);
        return posOrder;
    }

    /**
     * 修改数据
     *
     * @param posOrder 实例对象
     * @return 实例对象
     */
    @Override
    public PosOrder update(PosOrder posOrder) {
        this.posOrderMapper.update(posOrder);
        return this.queryOrderWithItemListByOrderId(posOrder.getOrderId());
    }

    /**
     * 通过主键删除数据
     *
     * @param orderId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer orderId) {
        return this.posOrderMapper.deleteById(orderId) > 0;
    }

    /**
     * 通过orderNo查询订单和产品列表
     *
     * @param orderNo
     * @return
     */
    public PosOrder queryOrderWithItemListByOrderNo(String orderNo){

        //通过orderNo查询orderId（主键）
        PosOrder posOrder = posOrderMapper.queryIdByOrderNo(orderNo);
        Integer orderId = posOrder.getOrderId();

        return posOrderMapper.queryOrderWithItemListByOrderId(orderId);
    }

    /**
     * 根据订单编号获取订单id（主键）
     *
     * @param orderNo
     * @return
     */
    public PosOrder queryIdByOrderNo(String orderNo){
        return posOrderMapper.queryIdByOrderNo(orderNo);
    }

}
